from metatags.models import Metatag

class MetatagMiddleware(object):
    """
    This middleware adds on the appropriate meta tags into the head
    section of a text/html document
    """
    def process_response(self, request, response):
        # Returns right out if bad status_code or not an text/html document
        if response.status_code != 200 or not \
          response.get('Content-Type','').lower().startswith('text/html'):
            return response
        # Try to get the tag, otherwise just return
        try:
            tag = Metatag.objects.get(url=request.path)
        except:
            return response
        # Parse out the response content
        for i,part in enumerate(response._container):
            # Parse each line of the content
            lines = part.splitlines()
            for j,line in enumerate(lines):
                # Look for first instance of <head>
                if line.find('<head>')>-1:
                    # Insert tags right after the <head> section
                    lines[j] = line.replace('<head>','<head>%s'%tag.tags())
                    # Pack up the response
                    response._container[i] = '\r\n'.join(lines)
                    # Delete the now unnecessary variables
                    del lines,line,tags,part,i,j
                    # Return modified response
                    return response
        # Return original response just in case
        return response